//使用Express框架

var express = require("express");
var bodyParser = require("body-parser"); //用于处理 JSON, Raw, Text 和 URL 编码的数据
var app = express();

//设置静态文件路径
app.use("/public", express.static("public"));

app.get("/", function(req, res) {
    res.send("Hello World");
});

app.get("/index.html", function(req, res) {
    res.sendFile(__dirname + "/" + "views/index.html");
});

app.get("/process_get", function(req, res) {
    // 输出 JSON 格式
    var response = {
        first_name: req.query.first_name,
        last_name: req.query.last_name,
    };
    console.log(response);
    res.writeHead(200, { "Content-Type": "text/plain;charset=utf8" });
    res.end(JSON.stringify(response));
});

// 创建 application/x-www-form-urlencoded 编码解析
var urlencodedParser = bodyParser.urlencoded({ extended: false });
app.post("/process_post", urlencodedParser, function(req, res) {
    // 输出 JSON 格式
    var response = {
        first_name: req.body.first_name,
        last_name: req.body.last_name,
    };
    console.log(response);
    res.end(JSON.stringify(response));
});

app.post("/file_upload", function(req, res) {
    console.log(req.files[0]); // 上传的文件信息

    var des_file = __dirname + "/" + req.files[0].originalname; //写入的文件路径
    fs.readFile(req.files[0].path, function(err, data) {
        fs.writeFile(des_file, data, function(err) {
            if (err) {
                console.log(err);
            } else {
                response = {
                    message: "File uploaded successfully",
                    filename: req.files[0].originalname,
                };
            }
            console.log(response);
            res.end(JSON.stringify(response));
        });
    });
});

var server = app.listen(8888, "127.0.0.1", function() {
    var host = server.address().address;
    var port = server.address().port;
    console.log("应用实例，访问地址为 http://%s:%s", host, port);
});